package com.example.account.controller;

import com.example.account.dto.UserReq;
import com.example.account.entity.User;
import com.example.account.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/users")
    public List<User> getUsers() {
        return userService.list();
    }

    /**
     * 新建用户信息
     * @param userReq userReq
     * @return 用户信息
     */
    @PostMapping("/users")
    public User createUser(@RequestBody @Validated UserReq userReq) {
        return userService.createUser(userReq);
    }

    /**
     * 更新用户信息
     * @param userReq userReq
     * @return user
     */
    @PutMapping("/users")
    public User updateUser(UserReq userReq) {
        return userService.updateUser(userReq);
    }

    /**
     * 根据id获取用户信息
     * @param id 用户id
     * @return user
     */
    @GetMapping("/users/{id}")
    public User getUserById(@PathVariable long id){
        return userService.getUserById(id);
    }
}
